package com.xinbochuang.machine.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinbochuang.machine.admin.domain.MacCheckRecord;
import com.xinbochuang.machine.admin.domain.Macinout;
import com.xinbochuang.machine.admin.domain.Worker;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.MacCheckRecordMapper;
import com.xinbochuang.machine.admin.service.IMacCheckRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.service.system.ISysUserService;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author 张亚辉
 * @date 2022-01-05
 */
@Service
public class MacCheckRecordServiceImpl extends ServiceImpl<MacCheckRecordMapper, MacCheckRecord> implements IMacCheckRecordService {
    @Resource
    ISysUserService iSysUserService;

    public boolean insertCheckRecord(String loginUserId,String state,String macInoutId) {
         boolean result = true;
        SysUser sysUser =null;
        if(StringUtils.isNotNull(loginUserId)&&!loginUserId.equals("null")){
            sysUser=iSysUserService.getById(Integer.valueOf(loginUserId));
        }else{
            sysUser = SecurityUtils.getLoginUser().getUser();
        }
        String recordUuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        MacCheckRecord macCheckRecord= new MacCheckRecord();
        macCheckRecord.setUuid(recordUuid);
        macCheckRecord.setCheckTime(DateUtil.now());
        macCheckRecord.setCheckUserId(sysUser.getUserId()+"");
        macCheckRecord.setCheckUserName(sysUser.getNickname());
        macCheckRecord.setState(state);
        macCheckRecord.setMacinoutId(macInoutId);
        macCheckRecord.setCreateTime(DateUtil.now());
        macCheckRecord.setCreateBy(sysUser.getNickname());

        //工单状态（1为待提交，2待审核，3为已驳回，4已审核， 5已验收， 6已终止） 0施工完成
        String stateName = "";
        String remark ="";
        if(StringUtils.isNotNull(state)){
            if(state.equals("1")){
                stateName="待提交";
                remark=sysUser.getNickname()+"  于"+DateUtil.now()+" 保存！";
            }else if(state.equals("2")){
                stateName="待审核";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 提交！";
            }else if(state.equals("3")){
                stateName="已驳回";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 驳回！";
            }else if(state.equals("4")){
                stateName="已审核通过";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 审核通过！";
            }else if(state.equals("0")){
                stateName="施工完成";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 施工完成！";
            }else if(state.equals("5")){
                stateName="已验收";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 验收！";
            }else if(state.equals("6")){
                stateName="已终止";
                remark=sysUser.getNickname()+" 于 "+DateUtil.now()+" 终止！";
            }
        }
        macCheckRecord.setStateName(stateName);
        macCheckRecord.setRemark(remark);
        save(macCheckRecord);
        return result;
    }


    public List<MacCheckRecord> getMacCheckRecordListByMacInoutId(String macInoutId) {
        QueryWrapper<MacCheckRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MacCheckRecord::getMacinoutId, macInoutId);
        queryWrapper.orderByAsc("CREATE_TIME");
        return baseMapper.selectList(queryWrapper);
    }
}
